<?php
/*
 * @灯商云端
 * @功能说明：用户中心
 * @更新说明：暂无更新
 * @文件名 UserController.class.php
 * @编码 UTF-8
 * @创建时间 2015-7-21 下午3:38:52
 * @创建人 XING
 */

namespace home\Controller;
use Think\Controller;

class MenuController extends ApibaseController {
    /*
     * 后台角色添加
     * @date: 2016-7-21下午5:06:13
     * @editor: yanyan
     */
    public function add(){
        if(I('post.submit')){
            if(I('post.id')){
                $effectId = M('Node')->where(array('id'=>I('post.id')))
                    ->setField(array(
                        'name' => I('post.name'),
                        'title' => I('post.title'),
                        'sort' => I('post.sort')?I('post.sort'):0,
                        'status' => 1,
                        'update_at' => time()
                    ));
                if($effectId){
                    $this->showtips('菜单更新成功');
                }else{
                    $this->showtips('操作失败');
                }
            }
            $data = array(
                'name' => I('post.name'),
                'title' => I('post.title'),
                'sort' => I('post.sort')?I('post.sort'):0,
                'status' => 1,
                'pid' => I('post.pid'),
                'add_at' => time(),
                'update_at' => time()
            );
            if(M('Node')->add($data)){
                $this->showtips('菜单添加成功');
            }else{
                $this->showtips('操作失败');
            }
        }
        if(I('get.id')){
            $menuData = M('Node')->where(array('id'=>I('get.id')))->find();
            $this->assign('menuData',$menuData);
        }
        $pid = I('get.pid')?I('get.pid'):0;
        $this->assign('pid',$pid);
        $this->display('menu/add');
    }


    /**
     * 菜单管理
     */
    public function index(){
        $pid  = I('get.pid')?:0;
        $data = M('Node')->where(['pid'=>$pid])->select();
        foreach ($data as $key => $value) {
            $data[$key]['next'] = M('Node')->where(['pid'=>$value['id']])->find();
        }
        $this->assign('data',$data);
        $this->display('menu/index');
    }

    /**
     * 删除菜单
     */
    public function deRole(){
        if(I('get.id')){
            M('Node')->where(['id'=>I('get.id')])->delete();
            $this->showtips('删除成功');
        }
    }
}